#include "E_Weapon_Maneger.h"

E_Weapon_Maneger::E_Weapon_Maneger(void) : p_EWeaponHead(NULL)
{
}

E_Weapon_Maneger::~E_Weapon_Maneger(void)
{
}

E_Weapon* E_Weapon_Maneger::Get_Last()
{
	if( !p_EWeaponHead )	return NULL;

	E_Weapon* pTmp = p_EWeaponHead;

	for( ; pTmp->Get_Next() ; )
	{
		pTmp = pTmp->Get_Next();
	}
	return pTmp;
}

void E_Weapon_Maneger::Create(int x, int y)
{
	E_Weapon* pTmp = new E_Weapon( x, y );

	if( !p_EWeaponHead )	p_EWeaponHead = pTmp;
	else
	{
		E_Weapon *pLast = this->Get_Last();
		pLast->Set_Next(pTmp);
		pTmp->Set_Prev(pLast);
	}
}
void E_Weapon_Maneger::Delete_All()
{
	E_Weapon* pTmp = p_EWeaponHead;

	for( ; pTmp ; )
	{
		pTmp->Shape_Delete();
		pTmp = this->Delete(pTmp);
		if( !pTmp )	break;
	}
	p_EWeaponHead = NULL;
}
E_Weapon* E_Weapon_Maneger::Delete(E_Weapon* pTmp)
{
	if ( !p_EWeaponHead ) return NULL;

	E_Weapon* pNext = pTmp->Get_Next();
	E_Weapon* pPrev = pTmp->Get_Prev();

	if( !pPrev && !pNext )
		p_EWeaponHead	=	NULL;
	else if( !pPrev && pNext )
	{
		p_EWeaponHead	=	pTmp->Get_Next();	
		p_EWeaponHead->Set_Prev(NULL);
	}
	else if( pPrev && pNext )
	{
		pPrev->Set_Next(pNext);
		pNext->Set_Prev(pPrev);
	}
	else if( pPrev && !pNext )
		pPrev->Set_Next(NULL);

	delete pTmp;
	return pNext;
}

void E_Weapon_Maneger::render()
{
	if( !p_EWeaponHead ) return;

	E_Weapon* pTmp = p_EWeaponHead;
	
	for( ; pTmp ; )
	{
		pTmp->render();
		pTmp = pTmp->Get_Next();
	}
}
void E_Weapon_Maneger::frameMove()
{
	if( !p_EWeaponHead ) return;

	E_Weapon* pTmp = p_EWeaponHead;
	
	for( ; pTmp ; )
	{
		if( !pTmp->frameMove())
		{
			pTmp->Shape_Delete();
			pTmp = this->Delete(pTmp);
		}
		else pTmp = pTmp->Get_Next();
	}
}